//
//  PrescriptionController.m
//  HarmonizePub
//
//  Created by rzhy on 2017/1/3.
//  Copyright © 2017年 Zy. All rights reserved.
//

#import "PrescriptionController.h"
#import "Config.h"
#import <UIScrollView+EmptyDataSet.h>
#import "NSString+Zy.h"
#import "Patient.h"
#import "PrescriptionDetailController.h"

@interface PrescriptionController () <UITableViewDataSource, UITableViewDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
{
    NSDictionary *_dict;
    NSArray *_dataArray;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation PrescriptionController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSString *title = @"门诊用药";
    if (self.dataDict) title = @"门诊处方";
    self.title = title;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    [self getMzzdData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 门诊处方
- (void)getMzzdData {
    Patient *patient = [Patient sharedPatient];
    NSString *path = XHUserGetMzyy;
    NSDictionary *param = @{@"id" : @(patient.id)};
    NSDictionary *cookie = @{CookieVaule : [UserDefaultsTool stringForKey:XHToken]};
    if (self.dataDict) {
        path = XHUserGetMzcf01;
        param = @{@"jzxh" : self.dataDict[@"jzxh"]};
    }
    [HTTPSTool POSTRequestWithPath:path cookie:cookie params:param success:^(NSDictionary * _Nonnull JSON) {
//        XHLog(@"门诊处方 -> JSON: %@", JSON);
        _dict = JSON[@"data"];
        _dataArray = _dict[@"list"];
        [_tableView reloadData];
    }];
}

#pragma mark - tableView delegate/dataSource Method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    NSDictionary *dict = _dataArray[indexPath.row];
    NSString *cflx = dict[@"cflx"];
    NSString *kfrq = [dict[@"kfrq"] stringInterceptWithBegin:0 end:10];
    NSString *text = [NSString stringWithFormat:@"%@\t\t%@", kfrq, cflx];
    cell.textLabel.text = text;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    NSDictionary *dict = _dataArray[indexPath.row];
    NSString *cfsb = dict[@"cfsb"];
    NSString *jzxh = self.dataDict[@"jzxh"];
    if (jzxh == nil) jzxh = _dict[@"jzxh"];
    PrescriptionDetailController *prescriptionDetail = [[PrescriptionDetailController alloc] init];
    prescriptionDetail.jzxh = jzxh;
    prescriptionDetail.cfsb = cfsb;
    [self.navigationController pushViewController:prescriptionDetail animated:YES];
    
}

#pragma mark - DZNEmptyDataSetSource Method
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    UIImage *image = [UIImage imageNamed:@"LoadFailure.png"];
    return image;
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *string = XHEmptyDes;
    NSAttributedString *abs = [[NSAttributedString alloc] initWithString:string attributes:nil];
    return abs;
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -64.0f;
}

#pragma mark - DZNEmptyDataSetDelegate Method
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    [self getMzzdData];
}

@end
